package com.andova.gltest

import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.andova.glcamera.device.CameraBuilder
import com.andova.glcamera.device.ANGLE_90
import com.andova.glcamera.device.RecorderBuilder
import com.andova.opengles.databinding.AcCaptureViewBinding
import java.io.File

class GlCaptureViewActivity : AppCompatActivity() {
    private lateinit var mBinding: AcCaptureViewBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = AcCaptureViewBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onPause() {
        super.onPause()
        mBinding.captureView.stop()
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onResume() {
        super.onResume()
        val path = File(Environment.getExternalStorageDirectory(), "capture_video.mp4").path
        val recorder = RecorderBuilder(path, 90, 30)
        val picture = File(Environment.getExternalStorageDirectory(), "capture_picture.jpg")
        mBinding.captureView.capture(picture, recorder)
                .confirm {
                    Toast.makeText(this, "toast confirm -> $it", Toast.LENGTH_SHORT).show()
                }
                .finish {
                    finish()
                }
                .useTextureRender(null, CameraBuilder.build().usePreviewCallback(true, angle = ANGLE_90))
                .start()
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (!hasFocus) return
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }
}